package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.dto.AnnouncementDto;
import com.project.dtoplus.AnnexPlus;
import com.project.dtoplus.AnnouncementDtoPlus;
import com.project.dtoplus.AnnouncementPage;
import com.project.entity.Annex;
import com.project.entity.Announcement;
import com.project.service.AnnexService;
import com.project.service.AnnouncementService;
import com.project.mapper.AnnouncementMapper;
import com.project.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * ClassName: AnnouncementServiceImpl
 * Package: com.project.service.impl
 * Description:
 *
 * @Author 看到风
 * @Create 2023/10/21 22:22
 * @Version 1.0
 */
@Service
@Slf4j
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, Announcement> implements AnnouncementService {

    @Autowired
    private AliOSSUtils aliOSSUtils;

    @Autowired
    private AnnexService annexService;


    @Autowired
    private  AnnouncementService announcementService;
    @Autowired
    private RedisTemplate redisTemplate;
    //1.添加公告
    @Override
    @Transactional
    public void saveWithAnnex( AnnouncementDto announcementDto)  {
        //1.保存基本信息在公告表

        announcementService.save(announcementDto);

        Long id = announcementDto.getId();
        for (Annex annex : announcementDto.getAnnex()) {
            annex.setAnnouncementAnnexId(id);
            annexService.save(annex);
        }



    }

    //2.修改公告时进行信息回填
    public AnnouncementDtoPlus getByIdWithAnnex(Long id){
        Announcement announcement=this.getById(id);


        AnnouncementDto announcementDto=new AnnouncementDto();
        AnnouncementDtoPlus announcementDtoPlus=new AnnouncementDtoPlus();

        BeanUtils.copyProperties(announcement,announcementDto);
        //查询公告所有附件
        LambdaQueryWrapper<Annex> annexLambdaQueryWrapper=new LambdaQueryWrapper<>();
        annexLambdaQueryWrapper.eq(Annex::getAnnouncementAnnexId,id);
        List<Annex> annexes = annexService.list(annexLambdaQueryWrapper);

        announcementDto.setAnnex(annexes);

        //信息筛选
        AnnouncementDtoPlus concise = this.getConcise(announcementDto);


        return concise;
    }

    //3.修改公告
    public void updateWithAnnex(AnnouncementDto announcementDto) throws IOException {
        //更新公告表
        this.updateById(announcementDto );

        //更新annex 表的delete操作
        LambdaQueryWrapper<Annex> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Annex::getAnnouncementAnnexId,announcementDto.getId());
        annexService.remove(queryWrapper);

        //redis删除
        String key="announcement_"+announcementDto.getId();
        redisTemplate.delete(key);


    }

    //4.删除公告
    public void deleteAnnex(String[] ids){
        for (String id:ids) {
              this.removeById(id);
              String key="announcement_"+id;
              redisTemplate.delete(key);
        }

    }
    //5.分页查询公告
    public Page pageSearch(int page, int pageSize, String title, String department , LocalDate modifiedTime){

        Page<Announcement> pageInfo =new Page<>(page,pageSize);

        Page<AnnouncementPage> announcementDtoPage =new Page<>();



        LambdaQueryWrapper<Announcement> queryWrapper=new LambdaQueryWrapper<>();

        queryWrapper.like(title!=null,Announcement::getTitle,title);
        queryWrapper.like(department!=null,Announcement::getDepartment,department);
        queryWrapper.like(modifiedTime!=null,Announcement::getModifiedTime,modifiedTime);

        //排序条件
        queryWrapper.orderByDesc(Announcement::getModifiedTime);

        //分页查询
        announcementService.page(pageInfo,queryWrapper);

        // 获取查询结果
        List<Announcement> announcements = pageInfo.getRecords();

        List<AnnouncementPage> list = announcements.stream().map(announcement -> {
            // 这里需要定义如何从Announcement转换到AnnouncementPage
            AnnouncementPage announcementPage=new AnnouncementPage();
            announcementPage.setId(announcement.getId());
            announcementPage.setTitle(announcement.getTitle());
            announcementPage.setModifiedTime(announcement.getModifiedTime());
            return announcementPage;


        }).collect(Collectors.toList());
        announcementDtoPage.setRecords(list);
        announcementDtoPage.setTotal(list.size());


        return announcementDtoPage;
    }

    //6.分页查询最新四条数据公告
    public Page searchLastFour(int page,int pageSize){
        Page<Announcement> pageInfo =new Page<>(page,pageSize);

        Page<AnnouncementPage> pageNew =new Page<>();
        LambdaQueryWrapper<Announcement> queryWrapper=new LambdaQueryWrapper<>();

        //排序条件
        queryWrapper.orderByDesc(Announcement::getModifiedTime);

        //分页查询
        announcementService.page(pageInfo,queryWrapper);
        // 获取查询结果
        List<Announcement> announcements = pageInfo.getRecords();

        List<AnnouncementPage> list = announcements.stream().map(announcement -> {
                    // 这里需要定义如何从Announcement转换到AnnouncementPage
            AnnouncementPage announcementPage=new AnnouncementPage();
            announcementPage.setId(announcement.getId());
            announcementPage.setTitle(announcement.getTitle());
            announcementPage.setModifiedTime(announcement.getModifiedTime());
            return announcementPage;


                }).collect(Collectors.toList());

        // 将转换后的结果设置到新的分页对象中
        pageNew.setRecords(list);
        pageNew.setCurrent(pageInfo.getCurrent());
        pageNew.setSize(pageInfo.getSize());
        pageNew.setTotal(4); // 总数为4，因为我们只查询前四条





        return pageNew;
    }

    //7.查询详情
    public AnnouncementDtoPlus getDetail(Long id){
        AnnouncementDto announcementDto=new AnnouncementDto();
        Announcement byId = this.getById(id);
        BeanUtils.copyProperties(byId,announcementDto);
        LambdaQueryWrapper<Annex> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Annex::getAnnouncementAnnexId,id);
        List<Annex> annexes=annexService.list(queryWrapper);
        announcementDto.setAnnex(annexes);
        AnnouncementDtoPlus concise = this.getConcise(announcementDto);
        return concise;
    }

    //8.添加公告附件
    public List<Annex> addAnnex(MultipartFile[] files) throws IOException {
        List<String> upload = aliOSSUtils.upload(files);

        List<Annex> annexes=new ArrayList<>();

        int i=0;
        for(MultipartFile file:files){

            String name=file.getOriginalFilename();
            Annex annex=new Annex();
            annex.setAnnex(upload.get(i));
            annex.setName(name);
            annexes.add(annex);
        }
        return annexes;

    }
    //信息筛选
    public AnnouncementDtoPlus getConcise(AnnouncementDto announcementDto){
        AnnouncementDtoPlus announcementDtoPlus=new AnnouncementDtoPlus();
        announcementDtoPlus.setId(announcementDto.getId());
        announcementDtoPlus.setDepartment(announcementDto.getDepartment());
        announcementDtoPlus.setTitle(announcementDto.getTitle());
        announcementDtoPlus.setContent(announcementDto.getContent());
        List<AnnexPlus> annex=announcementDto.getAnnex().stream().map((item)->{
            AnnexPlus annexPlus=new AnnexPlus();
            annexPlus.setUrl(item.getAnnex());
            annexPlus.setName(item.getName());
            return annexPlus;
        }).collect(Collectors.toList());
        announcementDtoPlus.setAnnex(annex);

        return announcementDtoPlus;
    }



}
